package com.ease.io.bio;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;

/**
 * 在cmd命令行中使用telnet localhost 9010
 * 可以连接上这个server
 *
 * @author: chenhs
 * @date: Created in 10:11 2019/12/23
 **/
public class BIOServer {

    private static Charset charset = Charset.forName("UTF-8");

    public static void main(String[] args) {
        int port = 9010;
        try (ServerSocket serverSocket = new ServerSocket(port)){
            while (true) {
                // 接收连接（阻塞-等待连接）
                Socket clientSocket = serverSocket.accept();
                BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), charset));
                String mess;
                // 读取数据（阻塞-等待数据读取）
                while ((mess = reader.readLine()) != null) {
                    System.out.println(mess);
                }
                clientSocket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
